// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-09 19:15
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.DynamicPlan;

import java.util.HashMap;

public class TwoSum {
    /**
     * 返回数组中给定答案数据的两个索引
     *
     * @param nums   数组
     * @param target 目标答案
     * @return 返回值
     */
    public int[] twoSum(int[] nums, int target) {
        // Hash查找
        int n = nums.length;
        int i = 0;
        int isExits = 0;
        HashMap<Integer, Integer> digits = new HashMap<>();
        for (; i < n; i++) {
            isExits = digits.getOrDefault(target - nums[i], -1);
            if (isExits == -1) {
                digits.put(nums[i], i);
            } else {
                break;
            }
        }
        return new int[]{i, isExits};
    }
}
